/**
 * @author 陈泰来
 * @version V1.0
 * @Project echo-java-base
 * @Title 天气数据
 * @Description *
 * @Package designmodel.observe.model
 * @date 2020/10/25 7:37 下午
 * @Copyright:学如逆水行舟，不进则退 .
 */
package designmodel.observe.model;

import designmodel.observe.theme.OutsideData;
import designmodel.observe.watcher.Observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherData implements OutsideData {

    private final List<Observer> observers = new ArrayList<>();

    //气温
    private float temperature;
    //湿度
    private float humidity;
    //压力
    private float pressure;


    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i = observers.indexOf(o);
        if (i > 0) {
            observers.remove(i);
        }
    }

    @Override
    public void notifyedObserver() {
        for (Observer observer : observers) {
            observer.update(temperature, humidity, pressure);
        }
    }

    //气候发生变化
    private void mesurementsChanged() {
        notifyedObserver();
    }

    //获取到新的气候变化值
    public void setMesurements(float t, float h, float p) {
        temperature = t;
        humidity = h;
        pressure = p;
        mesurementsChanged();
    }
}
